<?php

namespace web\common\command;

use addons\member\model\MemberAccountModel;
use web\common\model\sys\SysParameterModel;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\input\Argument;
use think\console\Output;
use think\Exception;
use think\Db;

class Import extends Command{

    /**
     * 定义命令
     */
    protected function configure(){
        $this->setName('Import')->setDescription('RUN ETH Data RPC API');
        $this->addArgument('action', Argument::REQUIRED, "The name of the function");
        //参数2
        $this->addArgument('param', Argument::OPTIONAL, "The name of the param");
    }
    
    protected function execute(Input $input, Output $output) {
        $action = $input->getArgument('action');
        $param = $input->getArgument('param');
        if (method_exists($this,$action)) {
            $this->$action($param);
        }
        else{
            echo "method is not exists\n";
        }
    }


    /**
     * 分类导入
     */
    public function classImport(){
        $goods_class = new \addons\shop\model\GoodsClass;
        $page = 0;
        $rows = 100;
        $list = $goods_class->table('sale_category')->limit($page*$rows,$rows)->select();
        while (count($list)>0) {
            foreach ($list as $k => $v) {
                $images = 'http://img1.100ysh.com'.$v['img'];
                if($v['img']) $res = $goods_class->where('id',$v['id'])->update(['image'=>$images]);
                echo $v['id']."\n";
                // $data = [
                //     'id'=>$v['id'],
                //     'pid'=>$v['p_id'],
                //     'class_name'=>$v['cate_name'],
                //     'level'=>$v['p_id']==0?1:$v['cate_3']==''?2:3,
                //     'is_show'=>1,
                //     'order_index'=>0,
                //     'update_time'=>NOW_DATETIME,
                // ];
                // $goods_class->add($data);
            }
            $page++;
            echo "page:{$page} \n";
            $list = $goods_class->table('sale_category')->limit($page*$rows,$rows)->select();
        }
        echo "done\n";
    }



    /**
     * 商品数据导入
     */
    public function dataImport(){
        //用户导入
        $goodsM = new \addons\shop\model\Goods();
        $unit_arr = (new \addons\shop\model\GoodsUnit)->column('unit_name,id');
        // dump($unit_arr);exit();
        $file = fopen('D:/goodsd.csv','r');
        $index = 0;
        while ($data = fgetcsv($file)) {
            if($index==0){
                foreach ($data as $key => &$v) {
                    $v = iconv('GBK','UTF-8',$v);
                }
                // dump($data);
                // exit();
            }
            // if($data[0]==1) continue;
            if($index>0){
                foreach ($data as $key => &$v) {
                    $v = iconv('GBK','UTF-8',$v);
                }
                $goods_code = $data[0];
                $ex = $goodsM->where('goods_code',$goods_code)->value('id');
                if($ex) continue;
                $info = [];
                $info['store_id'] = 0;
                $info['class_id'] = trim($data[4]);
                $info['unit_id'] = isset($unit_arr[$data[5]])?$unit_arr[$data[5]]:$data[5];
                $info['property_id'] = 6;
                $info['goods_type'] = 0;
                $info['auth_status'] = 1;
                $info['goods_code'] = $data[0];
                $info['goods_name'] = $data[6];
                $info['is_guige'] = 0;
                $info['stock'] = $data[7];
                $info['is_hot'] = 0;
                $info['market_price'] = $data[8];
                $info['price'] = $data[9];
                $info['freight_price'] = 0;
                $info['cost_price'] = $data[10];
                $info['detail'] = $data[11];
                $info['images'] = $data[12];
                $info['status'] = 0;
                $info['update_time'] = 0;
                $info['brand_id'] = 3;
                echo $data[0]."\n";
                $goodsM->add($info);
            }
            $index++;
        }
        fclose($file);
        echo "goods done\n";
    }
}